package com.fc.entities;

import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.EnumType
import javax.persistence.Enumerated
import javax.persistence.FetchType
import javax.persistence.JoinColumn
import javax.persistence.JoinTable
import javax.persistence.OneToMany
import javax.persistence.Table
import javax.validation.constraints.Max
import javax.validation.constraints.Min
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size

import com.fc.user.Gender

/**
 * 
 * @author Ács Ádám
 *
 */
@Entity
@Table(name = "User")
class User extends BaseEntity {
	@Column(name = "username", unique = true)
	@NotNull
	@Size(min=4, max=20)
	String userName

	@Column(name = "password")
	@NotNull
	@Size(min=4)
	String password

	@Column(name = "weight")
	@Min(20L)
	@Max(300L)
	@NotNull
	Integer weight

	@Column(name = "height")
	@Min(50L)
	@Max(250L)
	@NotNull
	Integer height

	@Column(name = "realname")
	@NotNull
	@Size(min=4, max=20)
	String realName

	@Column(name = "enabled")
	boolean enabled 

	@Column(name = "gender")
	@Enumerated(EnumType.STRING)
	@NotNull
	Gender gender

	@Column(name = "age")
	@Min(6L)
	@Max(99L)
	@NotNull
	Integer age

	@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
	@JoinTable(name = "User_Roles", joinColumns = [ @JoinColumn(name = "user_id", referencedColumnName="id") ], inverseJoinColumns = [ @JoinColumn(name = "role_id", referencedColumnName="id") ])
	Set<Role> roles = new HashSet<Role>()
	
	User() {
		initDefaults()
	}
	
	private void initDefaults() {
		enabled = true
	}
}